//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "MarketModelPathwiseDiscounter.h"
using namespace Cephei::QL::Models::Marketmodels;
#include <gen/QL/Math/Matrix.h>
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::CMarketModelPathwiseDiscounter::CMarketModelPathwiseDiscounter (Double paymentTime, Cephei::Core::IVector<Double>^ rateTimes) 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phMarketModelPathwiseDiscounter = NULL;
#endif
        QuantLib::Time _paymentTime = (QuantLib::Time)ValueHelper::Convert (paymentTime); //d
        rateTimes->Lock();
        INativeVector<Double>^ _NCIrateTimes = rateTimes->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCrateTimes = safe_cast<CDoubleVector^>(_NCIrateTimes);
        std::vector<QuantLib::Time>& _rateTimes = static_cast<std::vector<QuantLib::Time>&> (_NCrateTimes->GetReference ());
        _ppMarketModelPathwiseDiscounter = new boost::shared_ptr<QuantLib::MarketModelPathwiseDiscounter> (new QuantLib::MarketModelPathwiseDiscounter ( _paymentTime,  _rateTimes ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (rateTimes != nullptr) rateTimes->Unlock();    //not optional
    }
}
Cephei::QL::Models::Marketmodels::CMarketModelPathwiseDiscounter::CMarketModelPathwiseDiscounter (boost::shared_ptr<QuantLib::MarketModelPathwiseDiscounter>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMarketModelPathwiseDiscounter = NULL;
#endif
	_ppMarketModelPathwiseDiscounter = &childNative;
    
}
Cephei::QL::Models::Marketmodels::CMarketModelPathwiseDiscounter::CMarketModelPathwiseDiscounter (QuantLib::MarketModelPathwiseDiscounter& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phMarketModelPathwiseDiscounter = NULL;
#endif
	_ppMarketModelPathwiseDiscounter = new boost::shared_ptr<QuantLib::MarketModelPathwiseDiscounter> (&childNative);
    
    _MarketModelPathwiseDiscounterOwner = owner;
    
}

Cephei::QL::Models::Marketmodels::CMarketModelPathwiseDiscounter::CMarketModelPathwiseDiscounter (CMarketModelPathwiseDiscounter^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMarketModelPathwiseDiscounter = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppMarketModelPathwiseDiscounter = new boost::shared_ptr<QuantLib::MarketModelPathwiseDiscounter> (copy->GetShared());
        
    }
}
Cephei::QL::Models::Marketmodels::CMarketModelPathwiseDiscounter::CMarketModelPathwiseDiscounter (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMarketModelPathwiseDiscounter = NULL;
#endif
	if (!t->IsSubclassOf(CMarketModelPathwiseDiscounter::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::CMarketModelPathwiseDiscounter::CMarketModelPathwiseDiscounter (QuantLib::Handle<QuantLib::MarketModelPathwiseDiscounter>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phMarketModelPathwiseDiscounter = &childNative;
	_ppMarketModelPathwiseDiscounter = &static_cast<boost::shared_ptr<QuantLib::MarketModelPathwiseDiscounter>>(childNative.currentLink());
    
    _MarketModelPathwiseDiscounterOwner = owner;
}
Cephei::QL::Models::Marketmodels::CMarketModelPathwiseDiscounter::CMarketModelPathwiseDiscounter (QuantLib::Handle<QuantLib::MarketModelPathwiseDiscounter> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phMarketModelPathwiseDiscounter = &childNative;
	_ppMarketModelPathwiseDiscounter = &static_cast<boost::shared_ptr<QuantLib::MarketModelPathwiseDiscounter>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::CMarketModelPathwiseDiscounter::CMarketModelPathwiseDiscounter (QuantLib::MarketModelPathwiseDiscounter childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMarketModelPathwiseDiscounter = NULL;
#endif
	_ppMarketModelPathwiseDiscounter = new boost::shared_ptr<QuantLib::MarketModelPathwiseDiscounter> (new QuantLib::MarketModelPathwiseDiscounter (childNative));
    
}
#endif

Cephei::QL::Models::Marketmodels::CMarketModelPathwiseDiscounter::~CMarketModelPathwiseDiscounter ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppMarketModelPathwiseDiscounter != NULL)
    {
	    delete _ppMarketModelPathwiseDiscounter;
        _ppMarketModelPathwiseDiscounter = NULL;
    }
}
Cephei::QL::Models::Marketmodels::CMarketModelPathwiseDiscounter::!CMarketModelPathwiseDiscounter ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppMarketModelPathwiseDiscounter != NULL)
    {
	    delete _ppMarketModelPathwiseDiscounter;
    }
}
QuantLib::MarketModelPathwiseDiscounter& Cephei::QL::Models::Marketmodels::CMarketModelPathwiseDiscounter::GetReference ()
{
    if (_ppMarketModelPathwiseDiscounter == NULL) throw REFNEW NativeNullException ();
	return **_ppMarketModelPathwiseDiscounter;
}
boost::shared_ptr<QuantLib::MarketModelPathwiseDiscounter>& Cephei::QL::Models::Marketmodels::CMarketModelPathwiseDiscounter::GetShared ()
{
    if (_ppMarketModelPathwiseDiscounter == NULL) throw REFNEW NativeNullException ();
	return *_ppMarketModelPathwiseDiscounter;
}
QuantLib::MarketModelPathwiseDiscounter* Cephei::QL::Models::Marketmodels::CMarketModelPathwiseDiscounter::GetPointer ()
{
    if (_ppMarketModelPathwiseDiscounter == NULL) throw REFNEW NativeNullException ();
	return &**_ppMarketModelPathwiseDiscounter;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::MarketModelPathwiseDiscounter>& Cephei::QL::Models::Marketmodels::CMarketModelPathwiseDiscounter::GetHandle ()
{
	if (_phMarketModelPathwiseDiscounter == NULL)
	{
		_phMarketModelPathwiseDiscounter = new Handle<QuantLib::MarketModelPathwiseDiscounter> (*_ppMarketModelPathwiseDiscounter);
	}
	return *_phMarketModelPathwiseDiscounter;
}
#endif
bool Cephei::QL::Models::Marketmodels::CMarketModelPathwiseDiscounter::HasNative () 
{
	return (_ppMarketModelPathwiseDiscounter != NULL);
}

Cephei::QL::Models::Marketmodels::IMarketModelPathwiseDiscounter^ Cephei::QL::Models::Marketmodels::CMarketModelPathwiseDiscounter::GetFactors (Cephei::QL::Math::IMatrix^ LIBORRates, Cephei::QL::Math::IMatrix^ Discounts, UInt64 currentStep, Cephei::Core::IVector<Double>^ factors)
{
    CMatrix^ _CLIBORRates;
    CMatrix^ _CDiscounts;
    try
    {
        _CLIBORRates = safe_cast<CMatrix^> (LIBORRates);
        _CLIBORRates->Lock();
        QuantLib::Matrix& _LIBORRates = static_cast<QuantLib::Matrix&> (_CLIBORRates->GetReference ()); 
        _CDiscounts = safe_cast<CMatrix^> (Discounts);
        _CDiscounts->Lock();
        QuantLib::Matrix& _Discounts = static_cast<QuantLib::Matrix&> (_CDiscounts->GetReference ()); 
        QuantLib::Size _currentStep = (QuantLib::Size)ValueHelper::Convert (currentStep); //a
        factors->Lock();
        INativeVector<Double>^ _NCIfactors = factors->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCfactors = safe_cast<CDoubleVector^>(_NCIfactors);
        std::vector<QuantLib::Real>& _factors = static_cast<std::vector<QuantLib::Real>&> (_NCfactors->GetReference ());
    	(*_ppMarketModelPathwiseDiscounter)->getFactors ( _LIBORRates,  _Discounts,  _currentStep,  _factors );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CLIBORRates != nullptr) _CLIBORRates->Unlock();
        if (_CDiscounts != nullptr) _CDiscounts->Unlock();
        if (factors != nullptr) factors->Unlock(); //NO2  
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Models::Marketmodels::IMarketModelPathwiseDiscounter^ Cephei::QL::Models::Marketmodels::CMarketModelPathwiseDiscounter_Factory::Create (Double paymentTime, Cephei::Core::IVector<Double>^ rateTimes)
{
    return REFNEW CMarketModelPathwiseDiscounter ( paymentTime,  rateTimes);
}
